using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The ClassRoomLocationTableDB class is responsible for interacting with the database to retrieve and store information
/// about ClassRoomLocationTable objects.
/// </summary>
public class ClassRoomLocationTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of ClassRoomLocationTable from the underlying datasource.
/// </summary>
/// <param name="classRoomLocationKey">The unique ClassRoomLocationKey of the ClassRoomLocationTable in the database.</param>
/// <returns>An ClassRoomLocationTable when the ClassRoomLocationKey was found in the database, or null otherwise.</returns>
public static ClassRoomLocationTable GetItem(int classRoomLocationKey)
{
ClassRoomLocationTable myClassRoomLocationTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocClassRoomLocationTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@classRoomLocationKey", classRoomLocationKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myClassRoomLocationTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myClassRoomLocationTable;
}
}

/// <summary>
/// Returns a list with ClassRoomLocationTable objects.
/// </summary>
/// <returns>A generics List with the ClassRoomLocationTable objects.</returns>
public static ClassRoomLocationTableList GetList()
{
ClassRoomLocationTableList tempList = new ClassRoomLocationTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocClassRoomLocationTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with ClassRoomLocationTable objects.
/// </summary>
/// <returns>A generics List with the ClassRoomLocationTable objects.</returns>
public static ClassRoomLocationTableList GetListByClassRoomKey(int ClassRoomKey)
{
ClassRoomLocationTableList tempList = new ClassRoomLocationTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocClassRoomLocationTableSelectListByClassRoomKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@classRoomKey", ClassRoomKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with ClassRoomLocationTable objects.
/// </summary>
/// <returns>A generics List with the ClassRoomLocationTable objects.</returns>
public static ClassRoomLocationTableList GetListByLocationKey(int LocationKey)
{
ClassRoomLocationTableList tempList = new ClassRoomLocationTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocClassRoomLocationTableSelectListByLocationKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@locationKey", LocationKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with ClassRoomLocationTable objects.
/// </summary>
/// <returns>A generics List with the ClassRoomLocationTable objects.</returns>
public static ClassRoomLocationTableList GetListBySupervisorUserAccountKey(int SupervisorUserAccountKey)
{
ClassRoomLocationTableList tempList = new ClassRoomLocationTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocClassRoomLocationTableSelectListBySupervisorUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@supervisorUserAccountKey", SupervisorUserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a ClassRoomLocationTable in the database.
/// </summary>
/// <param name="myClassRoomLocationTable">The ClassRoomLocationTable instance to save.</param>
/// <returns>The new ClassRoomLocationKey if the ClassRoomLocationTable is new in the database or the existing ClassRoomLocationKey when an item was updated.</returns>
public static int Save(ClassRoomLocationTable myClassRoomLocationTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocClassRoomLocationTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myClassRoomLocationTable.ClassRoomLocationKey == -1)
{myCommand.Parameters.AddWithValue("@classRoomLocationKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@classRoomLocationKey", myClassRoomLocationTable.ClassRoomLocationKey);
}
if (myClassRoomLocationTable.LocationKey == null){
myCommand.Parameters.AddWithValue("@locationKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@locationKey", myClassRoomLocationTable.LocationKey);
}
if (myClassRoomLocationTable.ClassRoomKey == null){
myCommand.Parameters.AddWithValue("@classRoomKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@classRoomKey", myClassRoomLocationTable.ClassRoomKey);
}
if (myClassRoomLocationTable.SupervisorUserAccountKey == null){
myCommand.Parameters.AddWithValue("@supervisorUserAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@supervisorUserAccountKey", myClassRoomLocationTable.SupervisorUserAccountKey);
}
if (string.IsNullOrEmpty(myClassRoomLocationTable.Remark))
{
myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remark", myClassRoomLocationTable.Remark);
}
if (myClassRoomLocationTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myClassRoomLocationTable.Status);
}
if (myClassRoomLocationTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myClassRoomLocationTable.CreatedDateTime);
}
if (myClassRoomLocationTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myClassRoomLocationTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a ClassRoomLocationTable from the database.
/// </summary>
/// <param name="classRoomLocationKey">The ClassRoomLocationKey of the ClassRoomLocationTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int classRoomLocationKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocClassRoomLocationTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@classRoomLocationKey", classRoomLocationKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the ClassRoomLocationTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static ClassRoomLocationTable FillDataRecord(IDataRecord myDataRecord )
{
ClassRoomLocationTable myClassRoomLocationTable = new ClassRoomLocationTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ClassRoomLocationKey")))
{
myClassRoomLocationTable.ClassRoomLocationKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("ClassRoomLocationKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LocationKey")))
{
myClassRoomLocationTable.LocationKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("LocationKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ClassRoomKey")))
{
myClassRoomLocationTable.ClassRoomKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("ClassRoomKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SupervisorUserAccountKey")))
{
myClassRoomLocationTable.SupervisorUserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SupervisorUserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
{
myClassRoomLocationTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myClassRoomLocationTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myClassRoomLocationTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myClassRoomLocationTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myClassRoomLocationTable;
}
}

 } 
